/**
 * 自定义事件扩展
 */
;layui.define(['jquery', 'layer', 'form', 'table', 'methods', 'notify', 'upload'], function (exports) {
    var o = layui.jquery,
        f = layui.form,
        u = layui.util,
        methods = layui.methods,
        upload = layui.upload,
        ly = layui.layer,
        notify = layui.notify,
        handle = {
            // +----------------------------------------------------------------------
            // | 自定义 - 预加载方法
            // +----------------------------------------------------------------------
            preload: {
                importOrder: function (that, options, method) {
                    upload.render({
                        elem: that,
                        url: options.uploadUrl,
                        accept: 'file',
                        acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        exts: 'xls|xlsx',
                        before: function (obj) {
                            methods.load('上传中...');
                        },
                        done: function (data) {
                            methods.closeLoad();
                            if (data.code === 1) {
                                methods.load('上传成功，正在导入数据...');
                                setTimeout(function () {
                                    return methods.fetchPostThrottle(options.url, {file: data.data.url}, function (d) {
                                        methods.closeLoad();
                                        d.code === 1 ? notify.success(d.msg, 1000, function () {
                                            location.reload();
                                        }) : notify.error(d.msg)
                                    });
                                }, 200);
                            }
                        },
                        error: function () {
                            ly.msg('上传失败')
                        }
                    });
                }
            },
            // +----------------------------------------------------------------------
            // | 自定义顶部按钮方法
            // +----------------------------------------------------------------------
            top: {
                exportOrder: function (that, options, method) {
                    var ids = method.getTableCheck(), fVal = method.getSearchFormVal();
                    fVal.tableExportIds = ids.length > 0 ? ids : '';
                    methods.fetchGet(options.url, fVal, function (d) {
                        methods.closeLoad();
                        u.openWin({url: d.data})
                    });
                }
            },
            // +----------------------------------------------------------------------
            // | 自定义右侧按钮方法
            // +----------------------------------------------------------------------
            right: {
                login: function (that, data, options) {
                    methods.confirm('确定要登录吗？', () => {
                        methods.fetchPost(options.url, {id: data.id}, (e) => {
                            if (e.code === 1) {
                                parent.layer.open({
                                    type: 2,
                                    area: ['390px', o(window).height() < 800 ? (90 + '%') : (800 + 'px')],
                                    anim: 2,
                                    offset: 'rb',
                                    shade: 0,
                                    title: '<a  href="' + e.data.url + '" target="_blank" >当前登录 《 ' + e.data.code + '》 右键可选择新窗口打开</a>',
                                    content: "" + e.data.url + "",
                                });
                            } else {
                                ly.msg(e.msg);
                            }
                        })
                    });
                },

                adminLogin: function (that, data, options) {
                    methods.confirm('确定要登录吗？', () => {
                        methods.fetchPost(options.url, {id: data.id}, (e) => {
                            if (e.code === 1) {
                                u.openWin({url: e.data});
                            } else {
                                ly.msg(e.msg);
                            }
                        })
                    });
                }
            }
        };

    exports('handle', handle);
});